package com.preskool.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.preskool.dto.ActionResult;
import com.preskool.po.PersonalFees;
import com.preskool.service.PersonalFeesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author swj
 * @Date 2021/1/7
 * @version1.0
 */
@RestController
public class PersonalFeesController {
    //注入ServiceMapper
    @Autowired(required = false)
    private PersonalFeesService personalFeesService;

    /**
     * 查询全部数据,进行分页
     * @return
     */
    @RequestMapping(value = "/findAllPf",method = RequestMethod.GET)
    public ActionResult findAll(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "8") Integer pageSize){

        PageHelper.startPage(currentPage,pageSize);
        List<PersonalFees> all = personalFeesService.findAllPf(currentPage,pageSize);
        //在控制台输出分页信息
        PageInfo<PersonalFees> pif = new PageInfo<PersonalFees>(all);
        System.out.println("每页显示的条数:"+pif.getPageSize());
        System.out.println("当前页码:"+pif.getPageNum());
        System.out.println("总记录数:"+pif.getTotal());
        System.out.println("总页数:"+pif.getPages());
        //判断查询结果是否为空
        if(all!=null){
            return new ActionResult(200,"查询成功",all);
        }
        return new ActionResult(200,"查询失败",null);
    }

    /**
     * 通过id查询单条数据
     * @param pid
     * @return
     */
    @RequestMapping(value = "/findOnePf",method = RequestMethod.GET)
    public ActionResult findOne(Integer pid){
        PersonalFees one = personalFeesService.findOnePf(pid);
        if(one!=null){
            return new ActionResult(200,"查询成功",one);
        }
        return new ActionResult(200,"查询失败",null);
    }

    /**
     * 修改数据
     * @param personalFees
     * @return
     */
    @RequestMapping(value = "/updatePf",method = RequestMethod.POST)
    public ActionResult update(PersonalFees personalFees){
        //将前端传来的数据进行分解,封装到新的对象中
        PersonalFees PF = new PersonalFees();
        //判断前端传来的状态码是否为1,缴纳日期是否为空,1代表已缴纳,缴纳日期也不能为空
        if(personalFees.getStatus()!=1 && personalFees.getPaidDate()!=null){
            PF.setPid(personalFees.getPid());
            PF.setTid(personalFees.getTid()); //ID
            PF.setAccountName(personalFees.getAccountName());//姓名
            PF.setGender(personalFees.getGender());//性别
            PF.setFeesType(personalFees.getFeesType());//费用类型
            PF.setPaidDate(personalFees.getPaidDate());//缴纳日期
            PF.setAmount(personalFees.getAmount());//金额
            PF.setStatus(1);//状态码
        }
        int update = personalFeesService.updatePf(PF);
        if(update>-1){
            return new ActionResult(200,"修改成功",null);
        }
        return new ActionResult(203,"修改失败",null);
    }

    /**
     * 添加数据
     * @param personalFees
     * @return
     */
    @RequestMapping(value = "/addPf",method = RequestMethod.POST)
    public ActionResult add(PersonalFees personalFees){
        int add = personalFeesService.addPf(personalFees);
        if(add>-1){
            return new ActionResult(200,"添加成功",null);
        }
        return new ActionResult(203,"添加失败",null);
    }

    /**
     *
     * 删除数据
     * @param pid
     * @return
     */
    @RequestMapping(value = "/delPf",method = RequestMethod.GET)
    public ActionResult add(Integer pid){
        int delete = personalFeesService.delPf(pid);
        if(delete>-1){
            return new ActionResult(200,"删除成功",null);
        }
        return new ActionResult(203,"删除失败",null);
    }

}
